package com.lin.blog.controller;


import com.lin.blog.entity.Category;
import com.lin.blog.service.IArticleService;
import com.lin.blog.service.ICategoryService;
import com.lin.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lin
 * @since 2020-03-08
 */
@Api(tags = "Category接口")
@RestController
@RequestMapping("/category")
public class CategoryController {


    @Autowired
    private ICategoryService categoryService;




    /**
     * 获取树状CategoryList
     * @return
     */
    @ApiOperation("获取树状CategoryList")
    @GetMapping("/list/tree")
    public Result  getCategoryTree(){
        return categoryService.getTreeCategory();
    }




    /**
     * 通过主键ID查询Category
     * @param id
     * @return
     */
    @ApiOperation("通过主键ID查询用Category")
    @ApiImplicitParam(name = "id", value = "id", defaultValue = "0",required = true)
    @GetMapping("/{id}")
    public Result getCategoryById(@PathVariable(value = "id",required = true) Long id){
        return categoryService.getCategory(id);
    }

    /**
     * 插入一条Category记录
     * @param category
     * @return
     */
    @RequiresPermissions("category:add")
    @ApiOperation("插入一条Category记录")
    @PostMapping
    public Result insertCategory(@RequestBody @Valid Category category){
        return categoryService.saveCategory(category);
    }


    /**
     * 通过主键ID删除Category
     * @param id
     * @return
     */
    @RequiresPermissions("category:delete")
    @ApiOperation("通过主键ID删除Category")
    @ApiImplicitParam(name = "id", value = "id", defaultValue = "0",required = true)
    @DeleteMapping("/{id}")
    public Result deleteCategoryById(@PathVariable(value = "id",required = true) Long id){
        return categoryService.removeCategory(id);
    }

    /**
     * 通过主键ID更新Category
     * @param id
     * @param category
     * @return
     */
    @RequiresPermissions("category:update")
    @ApiOperation("通过主键ID更新Category")
    @ApiImplicitParam(name = "id", value = "id", defaultValue = "0",required = true)
    @PutMapping("/{id}")
    public Result updateCategory(@PathVariable(value = "id",required = true) Long id,@RequestBody Category category){
        return categoryService.updateCategory(id,category);
    }



    /**
     * 获取CategoryList
     * @return
     */
    @ApiOperation("获取CategoryList")
    @GetMapping("/list")
    public Result  getCategoryList(){
        return categoryService.listCategory();
    }



    /**
     * 分页获取CategoryList
     * @return
     */
    @ApiOperation("分页获取CategoryList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "第N页"),
            @ApiImplicitParam(name = "pageSize", value = "每页N条", defaultValue = "20")})
    @GetMapping("/list/{pageNum}/{pageSize}")
    public Result  getCategoryList(@PathVariable("pageNum")  Long pageNum,
                                  @PathVariable("pageSize") @RequestParam(value = "pageSize",defaultValue = "20") Long pageSize){
        return categoryService.listCategory(pageNum, pageSize);

    }




}
